﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;
using DevExpress.XtraExport;

namespace THSERP.Modules.Report
{
    public partial class RP022 : ReportForm
    {
        public THSList<ARInvoiceItemsInfo> lstInvoiceItem;
        int ICProductGroupID = 0;
       
        public RP022()
        {
            InitializeComponent();
            lstInvoiceItem = new THSList<ARInvoiceItemsInfo>();
            
        }

        private void fld_btnShowProductGroupTreeForm_Click(object sender, EventArgs e)
        {
            //int departmentID = Convert.ToInt32(fld_lkeFK_ICDepartmentID.EditValue);
            //guiProductGroupTree guiProductGroupTree = new guiProductGroupTree(departmentID);
            //guiProductGroupTree.Module = this.Module;
            //if (guiProductGroupTree.ShowDialog() == DialogResult.OK)
            //{
            //    ICProductGroupID = ((ICProductGroupsInfo)guiProductGroupTree.TreeList.GetSelectedObject()).ICProductGroupID;
            //    fld_txtFK_ICProductGroup.Text = guiProductGroupTree.GetSelectedProductGroupName();
            //    fld_lkeFK_ICDepartmentID.EditValue = guiProductGroupTree.ICDepartmentID;
            //}
        }

        private void RP002_Load(object sender, EventArgs e)
        {
            fld_dteSearchFromBRPOSDate.DateTime=THSApp.WorkingDate;
            fld_dteSearchToBRPOSDate.DateTime=THSApp.WorkingDate;
            fld_lkeHREmployees.Screen = this;
            fld_lkeHRShifts.Screen = this;
            fld_lkeHREmployees.InitializeControl();
            fld_lkeHRShifts.InitializeControl();
            fld_dgcBRRP022POSs.Screen = this;
            fld_dgcBRRP022POSs.InitializeControl();
            fld_dgcBRRP022POSItems.Screen = this;
            fld_dgcBRRP022POSItems.InitializeControl();
            fld_dgcBRRP022POSRedemptionItems.Screen = this;
            fld_dgcBRRP022POSRedemptionItems.InitializeControl();
            // fld_lblBranchName.Text = ((BRBranchsInfo)new BRBranchsController().GetObjectByID(THSApp.CurrentCompanyInfo.FK_BRBranchID)).BRBranchName;
            fld_btnApply_Click(new object(), new EventArgs());
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_btnApply_Click(object sender, EventArgs e)
        {
            lstInvoiceItem.Clear();
            DateTime from = Convert.ToDateTime(fld_dteSearchFromBRPOSDate.EditValue);
            DateTime to = Convert.ToDateTime(fld_dteSearchToBRPOSDate.EditValue);
            String strQuery = this.GenerateSearchQuery(THSUtil.GetTableNameFromBusinessObjectType(typeof(BRPOSsInfo)));
            DataSet ds = new BRPOSsController().GetDataSet(strQuery);
            fld_dgcBRRP022POSs.DataSource = ds.Tables[0];
            fld_dgcBRRP022POSs.RefreshDataSource();
            ((DevExpress.XtraGrid.Views.Grid.GridView)fld_dgcBRRP022POSs.Views[0]).BestFitColumns();
        }

        private void fld_btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_btnExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSave = new SaveFileDialog();
            dlgSave.Filter = "Microsoft Excel|*.xls";
            DialogResult dlgResult = dlgSave.ShowDialog();
            if (dlgResult == DialogResult.OK)
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
               // this.fld_dgcARRP002InvoiceItems.ExportToXls(dlgSave.FileName);
                this.FindForm().Refresh();
                ExportXlsProvider provider = new ExportXlsProvider(dlgSave.FileName);
                DevExpress.XtraGrid.Export.BaseExportLink link = this.fld_dgcBRRP022POSs.Views[0].CreateExportLink(provider);
                (link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
                link.ExportTo(true);
                provider.Dispose();
                Cursor.Current = currentCursor;
                if (DialogResult.Yes == MessageBox.Show("Xuất file Excel thành công! Bạn có muốn mở file ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    System.Diagnostics.Process.Start(dlgSave.FileName);
                }
            }
        }

        private void fld_btnPrint_Click(object sender, EventArgs e)
        {
            //this.fld_dgcARRP002InvoiceItems.PrintPriview();
            DevExpress.XtraPrinting.PrintingSystem printingSystem = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();

            try
            {
                //Set to landscape
                printingSystem.PageSettings.Landscape = true;
                this.Cursor = Cursors.WaitCursor;
                printLink.Component = this.fld_dgcBRRP022POSs;
                printLink.CreateDocument(printingSystem);
                printLink.VerticalContentSplitting = DevExpress.XtraPrinting.VerticalContentSplitting.Exact;
                printingSystem.PreviewFormEx.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
                printingSystem.Dispose();
                printLink.Dispose();
            }

        }
        public void InvalidatePOS(int iObjectiD)
        {
            DataSet dsPOSItems = new BRPOSItemsController().GetAllDataByForeignColumn("FK_BRPOSID", iObjectiD);
            fld_dgcBRRP022POSItems.DataSource = dsPOSItems.Tables[0];
            fld_dgcBRRP022POSItems.RefreshDataSource();
            //redemption item
            DataSet dsRedemptionItem = new BRPOSRedemptionItemsController().GetAllDataByForeignColumn("FK_BRPOSID", iObjectiD);
            fld_dgcBRRP022POSRedemptionItems.DataSource = dsRedemptionItem.Tables[0];
            fld_dgcBRRP022POSRedemptionItems.RefreshDataSource(); 
        }
    }
}